<?php

/*
 * $Author ：PHPYUN开发团队
 *
 * 官网: http://www.phpyun.com
 *
 * 版权所有 2009-2019 宿迁鑫潮信息技术有限公司，并保留所有权利。
 *
 * 软件声明：未经授权前提下，不得用于商业运营、二次开发以及任何形式的再次发布。
 */
class company_controller extends common
{

    function index_action()
    {
        $this -> get_moblie();
        
        $CacheM     =   $this -> MODEL('cache');
        
        $CacheList  =   $CacheM->GetCache(array('city', 'hy', 'com'));
        
        $this -> yunset($CacheList);
        
        if (intval($_GET['three_cityid'])) {
            
            $this   ->   yunset('cityname', $CacheList['city_name'][intval($_GET['three_cityid'])]);
            
        }

        foreach ($_GET as $k => $v) {
            
            if ($k != '') {
                
                $searchurl[] = $k.'='.$v;
                
            }
            
        }
        
        $this -> seo('firm');
        
        $searchurl  =   @implode('&', $searchurl);
        
        $this -> yunset('searchurl', $searchurl);
        $this -> yunset('backurl', Url('wap'));
        $this -> yunset('topplaceholder', '请输入企业关键字,如：有限公司...');
        $this -> yunset('headertitle', '公司搜索');
        $this -> yuntpl(array('wap/company'));
    }


    function show_action()
    {
        
        $this -> get_moblie();
        
        $UserinfoM  =   $this -> MODEL('userinfo');
        $companyM   =   $this -> MODEL('company');
        $jobM       =   $this -> MODEL('job');
        $schoolM    =   $this -> MODEL('school');
        
        $uid        =   intval($this -> uid);
        $cuid       =   intval($_GET['id']);
        
        $CacheM     =   $this -> MODEL('cache');
        $CacheList  =   $CacheM->GetCache(array('job', 'com', 'city', 'hy'));
        $this -> yunset($CacheList);
        
        $ComMember  =   $UserinfoM -> getInfo(array('uid'=> $cuid), array('field'=>'`status`'));
        
        $row        =   $companyM -> getInfo($cuid, array('logo' => '1'));
          
        $school_xjh        =   $schoolM -> getSchoolXjhList(array('uid'=>$uid, 'status'=>'1', 'limit'=>'3'));
        
        $this -> yunset('school_xjh', $school_xjh);
        
        $companymsg =   $companyM -> getCompanyMsgInfo(array('cuid'=>$cuid, 'status'=>'1'));
        
        $this -> yunset('companymsg', $companymsg);
        
        $show       =   $companyM -> getCompanyShowList(array('uid'=>$cuid, 'status'=>'0'));
       
        $this -> yunset('show', $show);

        if (!is_array($row)) {
            
            $this -> ACT_msg_wap($_SERVER['HTTP_REFERER'], '没有找到该企业！');
            
        } elseif ($ComMember[status] == 0) {
            
            $this -> ACT_msg_wap($_SERVER['HTTP_REFERER'], '该企业正在审核中，请稍后查看！');
            
        } elseif ($ComMember[status] == 3) {
            
            $this -> ACT_msg_wap($_SERVER['HTTP_REFERER'], '该企业未通过审核！');
            
        } elseif ($row[r_status] == 2) {
            
            $this -> ACT_msg_wap($_SERVER['HTTP_REFERER'], '该企业暂被锁定，请稍后查看！');
            
        }
        
        $linkphone  =   explode('-', $row['linkphone']);
        
        if (strlen($linkphone[0]) == 4) {
            
            $row['callphone']   =   $linkphone[0] . $linkphone[1];
            
        } else if (strlen($linkphone[0] > 8)) {
            
            $row['callphone']   =   substr($row['linkphone'], 0, 12);
            
        } else {
            
            $row['callphone']   =   $row['linkphone'];
            
        }
		 //联系方式
		$link		=$jobM->getCompanyTel(array('com_id'=>$row['uid'],'uid'=>$this->uid,'usertype'=>$this->usertype));
		
		$this->yunset('link',$link);

        $statisM    =   $this -> MODEL('statis');
        
        $rows       =   $statisM -> getInfo($cuid, array('usertype' => '2', 'field' => '`rating`'));
       
        $ratingM    =   $this -> MODEL('rating');
        
        $comrat     =   $ratingM -> getInfo(array('id'=> intval($rows['rating'])), array('pic'=>'1'));
        
        $row['lastupdate']  =   date('Y-m-d', $row['lastupdate']);
        
        if ($row['infostatus'] == '2') {
            
            $row['linkphone'] = $row['linktel'] = $row['linkmail'] = $row['linkqq'] = '';
            
        }
        
        $welfare    =   @explode(',', $row['welfare']);
        
        foreach ($welfare as $k => $v) {
            if (! $v) {
                unset($welfare[$k]);
            }
        }
        $row['welfare_n']   =   $welfare;
        
        // 解决通过Editor上传的图片路径问题
        $row['content']     =   str_replace(array('ti<x>tle','“','”'), array('title','',''), $row['content']);
        $row['content']     =   htmlspecialchars_decode($row['content']);
        
        preg_match_all('/<img(.*?)src=("|\'|\s)?(.*?)(?="|\'|\s)/', $row['content'], $res);
        
        if (! empty($res[3])) {
            
            foreach ($res[3] as $v) {
                
                if (strpos($v, 'http:') === false && strpos($v, 'https:') === false) {
                    $ossv=checkpic($v);
                    $row['content'] = str_replace($v, $ossv, $row['content']);
                }
            }
            
        }
        
        $this -> yunset("row", $row);
        
        $this -> yunset("comrat", $comrat);
        
        if ($uid && $this->usertype == '1') {
            
            $atnM   =   $this -> MODEL('atn');
            
            $isatn  =   $atnM -> getAtnInfo(array('uid' => $uid, 'sc_uid' => $cuid));
            
            $this   ->  yunset('isatn', $isatn);
            
            $userid_job =   $jobM -> getSqJobInfo(array('uid' => $uid, 'com_id' => $cuid));
            
            $this   ->  yunset('userid_job', $userid_job);
        }
        
        
        $data['company_name']       =   $row['name'];
        $data['company_name_desc']  =   $row['content'];
        $this -> data   =   $data;
        $this -> seo('company_index');
        $this -> yunset('headertitle', '公司详情');
        if($this->config['sy_h5_share']==1){
			$this -> yunset("shareurl", Url('wap', array('c' => 'company','a' => 'share','id' => $cuid)));
		}else{
			$this -> yunset("shareurl",Url('wap',array('c'=>'company','a'=>'show','id'=>$cuid)));	
		}
        
        // 距离
        $user_agent = (! isset($_SERVER['HTTP_USER_AGENT'])) ? FALSE : $_SERVER['HTTP_USER_AGENT'];
        
        if (($_COOKIE['mapx'] && $_COOKIE['mapx'] > 0) && ($_COOKIE['mapy'] && $_COOKIE['mapy'] > 0) && strpos($user_agent, 'Android') && is_weixin()) {
           
            $this->yunset(array(
                'mapx' => $_COOKIE['mapx'],
                'mapy' => $_COOKIE['mapy']
            ));
            
        } else {
            
            $this->yunset(array(
                'mapx' => 0,
                'mapy' => 0
            ));
            
        }
        $CompanyaccountM	=   $this -> MODEL('companyaccount');
		$departmentList		=	$CompanyaccountM -> getList(array('comid'=>intval($_GET['id'])),array('field'=>'`name`,`uid`'));
		$departmentjobs		=	$jobM -> getList(array('uid'=>intval($_GET['id']),'status'=>'0','state'=>'1','r_status'=>1),array('field'=>'`zuid`'));
		foreach($departmentjobs['list'] as $val){
			$zuids[]	=	$val['zuid'];
		}
		foreach($departmentList as $key=>$v){
			if(in_array($v['uid'], $zuids)){
				$departmentNames[]	=	$v['name'];
			}
		}
		$this->yunset("departmentNames",$departmentNames);
        $this->yuntpl(array('wap/company_show'));
    }
	function departmentjob_action(){
		$JobM				=	$this -> MODEL('job');
		$CompanyaccountM	=	$this -> MODEL('companyaccount');
		$where				=	array();
		$where['uid']		=	$_POST['comuid'];
		$where['status']	=	0;
		$where['state']		=	1;
		$where['r_status']	=	1;
		if($_POST['departmentName'] && $_POST['comuid']){
			$zzhuids	=	array();
			$zzhlist	=	$CompanyaccountM -> getList(array('comid'=>$_POST['comuid'],'name'=>$_POST['departmentName']),array('field'=>'`uid`'));
			if(is_array($zzhlist)){
				foreach($zzhlist as $val){
					$zzhuids[]	=	$val['uid'];
				}
			}
			$where['zuid']	=	array('in',@implode(",",$zzhuids));
		}
		$where['limit']		=	5;
		$where['orderby']	=	'lastupdate';
		$return		=	$JobM -> getList($where,array('isurl'=>'yes','utype'=>'wap','cache'=>'1'));
		$html	=	"";
		foreach($return['list'] as $v){
			$html .="<div style='padding-top:10px;' id='company_job_list'>";
				$html .="<div class='index_newedition_job_list' style='padding:15px 0px;border-bottom:1px solid #eee; margin-top:0px'>";
					$html .="<a href='".$v['joburl']."' target='_blank' title='".$v['name']."'>";
						$html .="<h3 class='index_newedition_jobname'>".$v['name']."</h3>";
						$html .="<div class='yun_newedition_jobinfo'>";
							$html.=$v['job_city_one']."-".$v['job_city_two'];
							if($v['job_exp']){
								$html .="<i class='yun_newedition_jobline'></i>".$v['job_exp']."经验";
							}
							if($v['job_edu']){
								$html .="<i class='yun_newedition_jobline'></i>".$v['job_edu']."学历";
							}
							$html .="<div class='yun_index_jobemergency_tg' style='right:0'>";
								if($v['urgent_time']>time()){
									$html .="<span class='yun_index_jobemergency'>急聘</span>";
								}
								if($v['rec']=='1' && $v['rec_time']>time()){
									$html .="<span class='yun_index_hot'>推荐</span>";
								}
							$html .="</div>";
							$html .="<span class='index_newedition_jobxz' style='right:0'>".$v['job_salary']."</span>";
							$html .="<div style='font-size:12px; padding-top:5px;'>";
								if($v['lastupdate_n']=="今天" || $v['lastupdate_n']=="昨天"){
									$html .="<span style='color:red;'>".$v['lastupdate_n']."</span>更新";
								}else{
									$html .=$v['lastupdate_n']."更新";
								}
							$html .="</div>";
						$html .="</div>";
					$html .="</a>";
				$html .="</div>";
			$html .="</div>";
		}
		echo $html;die;
	}
    
    function share_action()
    {
        
        $this   ->  get_moblie();

        $cuid       =   intval($_GET['id']);
        
        $comM       =   $this -> MODEL('company');
        
        $row        =   $comM -> getInfo($cuid, array('logo'=>'1'));

        $welfare    =   @explode(',', $row['welfare']);
        
        foreach ($welfare as $k => $v) {
        
            if (! $v) {
                
                unset($welfare[$k]);
            }
        }
        
        $row['welfare']     =   $welfare;
        
        $row['content']     =   strip_tags($row['content']);
        
        $jobM   =   $this->MODEL('job');
        
        $link   =   $jobM -> getCompanyTel(array('com_id'=>$row['uid'],'uid'=>$this->uid,'usertype'=>$this->usertype));
      
        $this->yunset('link',$link);
        
        $this -> yunset('row', $row);
        
        $show   =   $comM -> getCompanyShowList(array('uid' => $cuid), array('field' => '`picurl`'));
         
        $this -> yunset('show', $show);
        
        $product    =   $comM -> getCompanyProductList(array('uid' => $cuid, 'status' => '1'));
        
        $this -> yunset('product', $product);
        
        $CacheM     = $this->MODEL('cache');
        
        $CacheList  = $CacheM->GetCache(array('job', 'com', 'city', 'hy'));

        $this -> yunset($CacheList);
        
        $data['company_name']       =   $row['name'];
        $data['company_name_desc']  =   $row['content'];
        $this -> data   =   $data;
        
        $this -> seo('company_index');
        $this -> yunset('company_style', $this->config['sy_weburl'] . '/app/template/wap/company');
        $this -> yuntpl(array('wap/company/index'));
    }

    function msg_action()
    {
        $this -> get_moblie();
        
        $comM   =   $this -> MODEL('company');      
        $row    =   $comM -> getInfo(intval($_GET['id']));
        $this   ->  yunset('row', $row);
        
        $data['company_name']       =   $row['name'];
        $data['company_name_desc']  =   $row['content'];
        $this -> data   =   $data;
        $this -> seo('company_index');
        $this -> yunset('headertitle', '公司详情');
        $this -> yuntpl(array('wap/company_msg'));
    }

    function whb_action()
    {
        
        $this ->seo('whb');
        if ($this->uid) {
            $backurl    =  Url('wap', array(), 'member');
        }else{
            $backurl    =  Url('wap', array());
        }
       
        $this -> yunset('backurl', $backurl);
        $this ->yunset('headertitle', '企业微海报生成');
        $this -> yuntpl(array('wap/hb/whb'));
    }

    
    function ws_action()
    {
        
        if(!$this->uid){
            
            $this->ACT_msg_wap(Url('wap', array('c' => 'login')), '非法操作！', 2, 3);
            
        }else if($this->uid != $_GET['id']){
            
            $this->ACT_msg_wap(Url('wap',array(),'member'), '参数错误，请重试！', 2, 3);
        }
        
        if (! $this->config['sy_haibaouser'] || ! $this->config['sy_haibaouserid'] || ! $this->config['sy_haibaopw'] || ! $this->config['sy_haibaokey'] || $this->config['sy_haibao_isopen'] != '1') {
            
            $data['msg'] = '还没有配置海报，请联系管理员！';
            
            $data['url'] = Url('wap', array('c' => 'company', 'a' => 'whb', 'id' => $_GET['id']));
            
            $this -> yunset('layer', $data);
            
        }
        
        $this->seo('whb');
        $this -> yunset('backurl', Url('wap', array('c' => 'company', 'a' => 'whb', 'id' => $_GET['id'])));
        $this->yunset('headertitle', '海报下载');
        $this->yuntpl(array('wap/hb/ws'));
    }

    function hbshow_action()
    {
        if ($_GET['id']){
            
            $id     =   intval($_GET['id']);
            
            if(!$this->uid){
                
                $this->ACT_msg_wap(Url('wap', array('c' => 'login')), '请先登录！', 2, 3);
                
            }else if($this->uid != $id ){
                
                $this->ACT_msg_wap(Url('wap',array(),'member'), '参数错误，请重试！', 2, 3);
            }
        }elseif ($_GET['uu'] && $_GET['token']){
            
            $member  =  $this->yzToken($_GET['uu'], $_GET['token']);
            
            if ($member['errmsg']){
                
                $this->ACT_msg_wap(Url('wap'), $member['errmsg'], 2, 3);
            }else{
                
                $id  =  $member['uid'];
            }
        }
        if (!empty($id)){
            
            $CacheM     =   $this->MODEL('cache');
            
            $CacheList  =   $CacheM->GetCache(array('job', 'com', 'city'));
            
            $this       ->  yunset($CacheList);
            
            $jobM   =   $this -> MODEL('job');
            
            if ($_GET['jobids']) {
                $jobids  =  @explode(',', $_GET['jobids']);
            }
            
            $where  =   array();
            
            $where['uid']       =   $id;
            $where['status']    =   '0';
            $where['state']     =   '1';
            $where['orderby']   =   'lastupdate,desc';
            if ($jobids) {
                $where['id']    =   array('in', pylode(',', $jobids));
            }
            
            
            $jobA   =   $jobM -> getList($where);
            
            $this   ->  yunset('jobs', $jobA['list']);
            
            $comM   =   $this -> MODEL('company');
            
            $com    =   $comM -> getInfo($id, array('field' => '`name`'));
            
            $this   ->  yunset('comname', $com['name']);
            
            $hb     =   $_GET['hb'];
            
            $this   ->  yunset('hb', $hb);
        }else{
            
            $this->ACT_msg_wap(Url('wap'), '参数错误，请重试！', 2, 3);
        }
        
        $this->yuntpl(array('wap/hb/'.$hb.'/tpl'));
    }

    function getHb_action()
    {
        $CacheM     =   $this->MODEL('cache');
        
        $CacheList  =   $CacheM->GetCache(array('job', 'com', 'city'));

        $this       ->  yunset($CacheList);
        
        $jobM       =   $this -> MODEL('job');
        
        $id         =   intval($_GET['id']);

        $where              =   array();
        
        $where['uid']       =   $id;
        $where['status']    =   0;
        $where['state']     =   1;
        $where['orderby']   =   'lastupdate,desc';
        
        if ($_GET['jobids']) {
            
            $jobids         =   @explode(',', $_GET['jobids']);
            
            $where['id']    =   array('in', pylode(',', $jobids));
            
        }
        
        $jobA       =   $jobM -> getList($where);
        
        $this       ->  yunset('jobs', $jobA['list']);
       
        $comM       =   $this -> MODEL('company');
        
        $com        =   $comM -> getInfo($id, array('field' => '`name`'));
        
        $this       ->  yunset('comname', $com['name']);

        $hb         =   $_GET['hb'];
        
        $this -> yunset('hb', $hb);
        
        $this -> yuntpl(array('wap/hb/'.$hb.'/tpl'));
        
    }

    function getImg_action()
    {
        if (! $this->config['sy_haibaouser'] || ! $this->config['sy_haibaopw'] || ! $this->config['sy_haibaokey'] || $this->config['sy_haibao_isopen'] != '1') {
            
            echo json_encode(array(
                'errmsg' => '还没有配置海报，请联系管理员',
                'error' => 15
            ));
            
            die();
        }

        $mid    =   $this->config['sy_haibaouserid'];

        $id     =   intval($_GET['id']);
        $hb     =   intval($_GET['hb']);
        $jobids =   $_GET['jobids'] ? trim($_GET['jobids']) : '';
//      原有不加职位参数的
//      $url    =   'http://haibao.phpyun.com/make.php';
//      $url    .= '?mid=' . $mid . '.' . $id . '.' . $hb . '.' . $this->config['sy_haibaokey'];   
        
        $url    =   'http://haibao.phpyun.com/makehb.php';
        $url    .= '?mid=' . $mid . '.' . $id . '.' . $hb . '.' . $jobids . '.' . $this->config['sy_haibaokey'];
        
        if (extension_loaded('curl')) {
            
            $file_contents  =   CurlGet($url);
            
        } else if (function_exists('file_get_contents')) {
            
            $file_contents  = file_get_contents($url);
            
        }
        
        if ($file_contents) {
            
            echo $file_contents;
            
        } else {
            
            echo json_encode(array(
                'errmsg' => '网站不支持生成海报',
                'error' => - 1
            ));
            die();
        }
    }
    function yzToken($uid, $token)
    {
        if (!empty($uid) && !empty($token)){
            
            $userInfoM  =  $this->MODEL('userinfo');
            $member     =  $userInfoM->getInfo(array('uid'=>$uid),array('field'=>'`uid`,`username`,`usertype`,`password`,`salt`,`pid`,`did`,`status`'));
            if ($member){
                if ($member['status'] == 2){
                    return array('errmsg'=>'您的账号已被锁定，请联系网站客服');
                }
                $mdtoken  =  md5($member['username'].$member['password'].$member['salt'].$member['usertype']);
                
                if($token != $mdtoken){
                    
                    return array('errmsg'=>'登录信息有误，请重新登录');
                }else{
                    
                    $user  =  array(
                        'uid'       =>  $member['uid'],
                        'username'  =>  $member['username'],
                        'usertype'  =>  $member['usertype'],
                        'did'       =>  $member['did']
                    );
                    // 处理子账号
                    if (!empty($member['pid'])){
                        $user['uid']   =  $member['pid'];
                        $user['spid']  =  $member['uid'];
                    }
                    
                    return $user;
                }
            }else{
                return array('errmsg'=>'用户信息错误，请重新登录');
            }
        }else{
            return array('errmsg'=>'用户信息缺失');
        }
    }
}
?>